// ml:run = $bin < input
#include <iostream>
#include <cstring>
#include <vector>

int const maxn = 3000;
char map[maxn][maxn];
int co[maxn];
bool vis[maxn];
int n;
std::vector<int> g[maxn];

void init()
{
    for (int i = 0; i <= n; i++) g[i].clear();
}

void add_edge(int u, int v)
{
    g[u].push_back(v);
}

bool circle(int u)
{
    co[u] = 1; vis[u] = true;
    for (auto v : g[u]) {
        if (vis[v]) {
            if (co[v]) return true;
        } else {
            if (circle(v)) return true;
        }
    }
    co[u] = 0;
    return false;
}

bool not_ok(int u, int start, char ch)
{
    vis[u] = true;
    if (u != start && map[start][u] != ch) return true;
    for (auto v : g[u]) {
        if (vis[v]) continue;
        if (not_ok(v, start, ch)) return true;
    }
    return false;
}

bool work(char ch)
{
    init();
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            if (map[i][j] == ch) add_edge(i, j);
        }

    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < n; i++)
        if (circle(i)) return false;

    /* std::cerr << "fuck " << ch << "\n"; */

    for (int i = 0; i < n; i++) {
        memset(vis, 0, sizeof(vis));
        if (not_ok(i, i, ch)) return false;
    }
    return true;
}

bool check()
{
    for (int i = 0; i < n; i++)
        if (map[i][i] != '-') return false;
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++) {
            if (map[i][j] == '-' && map[j][i] == '-') return false;
            if (map[i][j] != '-' && map[j][i] != '-') return false;
        }
    return true;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    int T; std::cin >> T;
    while (T--) {
        std::cin >> n;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) std::cin >> map[i][j];
        if (!check()) {
            std::cout << "N\n";
            continue;
        }

        /* std::cerr << work('P') << "\n"; */
        /* std::cerr << work('Q') << "\n"; */

        if (work('P') && work('Q'))
            std::cout << "T\n";
        else
            std::cout << "N\n";
    }
}

